<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/vue.js"></script>
    <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script>
</head>
<body>
    <div id="app">
        <h1>hello</h1>
        <p>
            <!-- 渲染路径，最后变为A标签 -->
            <!-- <router-link to="/foo">go to foo</router-link>
            <router-link to="/bar">go to bar</router-link> -->
            <button @click="goToFoo">go to foo</button>
            <button @click="goToBar">go to bar</button>
        </p>
        <!-- 路由出口 -->
        <router-view></router-view>
    </div>

    <template id="foo">
        <div>
            <!-- 当通过id选择器选择到foo组件时，可显示test1组件 -->
            <!-- 将test1挂载在foo组件里 -->
            <div>foo</div>
            <router-view></router-view>
        </div>
    </template>
    <script>
        // 1.注册路由组件
        const Foo={template:`#foo`}
        const Bar={template:`<div>bar</div>`}
        const Test1={template:`<h1>test1111111111</h1>`}

        //2.匹配路由组件的路径
        const routes=[
            {path:"/foo",component:Foo,
            children:[
                //将test1的路径配置到foo组件里
                {path :"test1",component:Test1}
            ]
        },
            {path:"/bar",component:Bar}
        ]

        //3.创建router实例
        const router=new VueRouter({
            routes
        })
        
        //Vue实例对象
        var vm=new Vue({
            el:'#app',
            router,
            methods:{
                goToFoo(){
                    this.$router.push("/foo/test1")
                },
                goToBar(){
                    this.$router.replace("/bar")
                }
            }
            
        })

    </script>
</body>
</html>